package com.think.demo.base.thread;

import org.junit.Test;
import typeinfo.interfacea.A;

import java.util.concurrent.*;

public class TestThread {
	@Test
	public void 测试callable() {
		FutureTask<String> target = new FutureTask<>(new Callable<String>() {
			@Override
			public String call() throws Exception {
				Thread.sleep(1000);
				return "线程执行完毕";
			}
		});
		new Thread(target).start();
		try {
			System.out.println(target.get());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}

	public static void 测试死锁() {
		AA a = new AA();
		BB b = new BB();
		new Thread(new Runnable() {
			@Override
			public void run() {
				a.methodA(b);
			}
		},"线程A").start();

		new Thread(new Runnable() {
			@Override
			public void run() {
				b.methodB(a);
			}
		},"线程B").start();
	}


	public static void 线程池() {
//		ExecutorService executorService = Executors.newCachedThreadPool();
//		ExecutorService executorService1 = Executors.newSingleThreadExecutor();
//		ExecutorService executorService2 = Executors.newFixedThreadPool(5);
//		ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
//		ScheduledExecutorService scheduledExecutorService1 = Executors.newSingleThreadScheduledExecutor();
//		scheduledExecutorService.schedule(new Runnable() {
//			@Override
//			public void run() {
//				System.out.println("333333");
//			}
//		}, 3, TimeUnit.SECONDS);
//		scheduledExecutorService.shutdown();
		ThreadPoolExecutor executorService = (ThreadPoolExecutor) Executors.newFixedThreadPool(5, new ThreadFactory() {
			@Override
			public Thread newThread(Runnable r) {

				return new Thread(r);
			}
		});
//		for (int i = 0; i < 100; i++) {
//			int finalI = i;
//			executorService.execute(() -> {
//				System.out.println("正在执行task "+ finalI);
//				try {
//					Thread.currentThread().sleep(4000);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//				System.out.println("task "+ finalI +" 执行完毕");
//				System.out.println("线程池中线程数目："+executorService.getPoolSize()+"，队列中等待执行的任务数目："+
//						executorService.getQueue().size()+"，已执行玩别的任务数目："+executorService.getCompletedTaskCount());
//			});
//			executorService.submit(() -> {
//
//			});
//		}
		Future<String> submit = (Future<String>) executorService.submit(() -> {
			System.out.println("11111");
		});
		try {
			System.out.println(submit.get());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}

		Future<String> ssss = executorService.submit(() -> {
			System.out.println(2222);
		}, "ssss");
		try {
			System.out.println(ssss.get());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
//		测试死锁();
		线程池();
	}
}

class AA{
	public synchronized void methodA(BB b) {
		System.out.println("当前线程名：" + Thread.currentThread().getName()
				+ ",进入A类的methodA()方法");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("当前线程名：" + Thread.currentThread().getName()
				+ ",打算调用B类的b()方法");
		b.b();
	}

	public synchronized void a() {
		System.out.println("成功调用A类的a()方法");
	}
}

class BB{
	public synchronized void methodB(AA a) {
		System.err.println("当前线程名：" + Thread.currentThread().getName()
				+ ",进入B类的methodB()方法");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.err.println("当前线程名：" + Thread.currentThread().getName()
				+ ".打算调用A类的a()方法");
		a.a();
	}

	public synchronized void b() {
		System.err.println("成功调用B类的b()方法");
	}
}
